define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wechat/articles/index' + location.search,
                    add_url: 'wechat/articles/add',
                    edit_url: 'wechat/articles/edit',
                    del_url: 'wechat/articles/del',
                    multi_url: 'wechat/articles/multi',
                    import_url: 'wechat/articles/import',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'title',
                            title: __('Title'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {

                                var result = row.is_first ? "<b style='color:red'>" + row.title + "</b>" : row.title;

                                return result;
                            }
                        },
                        {
                            field: 'cover',
                            title: '封面',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                var result = "<a href=" + row.cover + " target='_blank'><img style='height: 50px;width:50px'  src='" + row.cover + "' class='cover'></a>";

                                return row.cover ? result : '';
                            }
                        },
                        {
                            field: 'author',
                            title: '作者',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'link',
                            title: '微信来源',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                if(row.gather){
                                    var result = "<a href=" + row.gather.link + " target='_blank'>" + row.gather.title + "</a>";

                                    return result;
                                }

                            }
                        },
                        {
                            field: 'imgs_count',
                            title: '图片数量',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {

                                return row.imgs ? row.imgs.split(',').length : 0;
                            }
                        },
                        {
                            field: 'summary',
                            title: '摘要',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'weigh',
                            title: '权重',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'state',
                            title: '状态',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                var result = '未知';
                                if (row.state === 0) {
                                    result = '待发布';
                                } else if (row.state === 1) {
                                    result = '草稿';
                                } else if (row.state === 2) {
                                    result = '已发布';
                                }
                                return result;
                                // var selectStr = "<select><option></option></select>";
                            }
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                // 这里可以直接判断  编辑和删除 按钮 隐藏  这是第一种
                                if (row.state !== 0) {
                                    $(table).data("operate-del", null);
                                }

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", "#btn-draft", function () {
                var ids = Table.api.selectedids(table, true);
                // var   selectedData = Table.api.selecteddata(table, true);
                if (ids.length >= 8) {
                    Backend.api.toastr.error('最多选择8条');
                    return
                }
                var index = layer.load();
                $.ajax({
                    url: "wechat/articles/makeDraft",
                    type: 'post',
                    dataType: 'json',
                    data: {ids: ids.join(',')},
                    success: function (ret) {
                        console.log('ret', ret)
                        ret.code === 0 && Backend.api.toastr.error(ret.msg);
                        ret.code === 1 && Backend.api.toastr.success(ret.msg);

                        layer.close(index);
                        table.bootstrapTable('refresh');

                    }, error: function (e) {
                        Backend.api.toastr.error(e.msg);
                        layer.close(index);
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
